﻿using System;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;
using SYDZ.WMS.DBUtility;

namespace ElectronicWMSA.Warehouse
{
    public partial class List_StockFrm : Form
    {
        private string strWhere = "1=1";
        public List_StockFrm()
        {
            InitializeComponent();
        }
        private void List_StockFrm_Load(object sender, EventArgs e)
        {
            pageControl2.PageCurrent = 1; pageControl2.PageSize = 100;
            label9.Text = "库存统计：PJ在库【 】、FP在库【 】、灯泡在库【 】、PROAV在库【 】、SP金具在库【 】、其它类型在库【 】";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(500); GetSUM(); }, 30); wf.ShowDialog(this);
            if (dt.Rows.Count > 0)
            {
                this.label9.Text = "库存统计：PJ在库【 " + dt.Rows[0][0].ToString() + " 】、" +
                   "FP在库【 " + dt.Rows[1][0].ToString() + " 】、" +
                   "灯泡在库【 " + dt.Rows[2][0].ToString() + " 】、" +
                   "PROAV在库【 " + dt.Rows[3][0].ToString() + " 】、" +
                   "SP金具在库【 " + dt.Rows[4][0].ToString() + " 】、" +
                   "其它类型在库【 " + dt.Rows[5][0].ToString() + " 】";
            }
        }
        #region 数据
        private int DBindData()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "T_Stock";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl2.PageCurrent;
            pageData.PageSize = this.pageControl2.PageSize;
            pageData.QueryCondition = strWhere;
            pageData.QueryFieldName = "*";
            WaitinglodingFrm wf = new WaitinglodingFrm((obj, args) => { Thread.Sleep(500); pageData.QueryDataTable(); }, 500); wf.ShowDialog(this);
            pageControl2.bindingSource.DataSource = pageData.dsInfo.Tables[0];
            pageControl2.bindingNavigator.BindingSource = pageControl2.bindingSource;
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = this.pageControl2.bindingSource;
            dataGridView2.ClearSelection();
            dataGridView2.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl2_EventPaging(Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData();
        }
        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView2.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView2.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        DataTable dt = new DataTable();
        private void GetSUM()
        {
            string str1 = " " +
                "SELECT SUM(convert(int,Bk06)) AS KC FROM T_Stock WHERE BK03='PJ'       and PickList_NO is null AND BK11 IS NULL UNION ALL " +
                "SELECT SUM(convert(int,Bk06)) AS KC FROM T_Stock WHERE BK03='FP'       and PickList_NO is null AND BK11 IS NULL UNION ALL " +
                "SELECT SUM(convert(int,Bk06)) AS KC FROM T_Stock WHERE BK03='Option'   and PickList_NO is null                  UNION ALL " +
                "SELECT SUM(convert(int,Bk06)) AS KC FROM T_Stock WHERE BK03='PROAV'    and PickList_NO is null AND BK11 IS NULL UNION ALL " +
                "SELECT SUM(convert(int,Bk06)) AS KC FROM T_Stock WHERE BK03='SP金具'   and PickList_NO is null AND BK11 IS NULL UNION ALL " +
                "SELECT SUM(convert(int,Bk06)) AS KC FROM T_Stock WHERE BK03='其它类型' and PickList_NO is null AND BK11 IS NULL           ";
            dt = DbHelperSQL.ExecuteDataTable(str1);
        }
        #endregion

        private void btnSearch_Click(object sender, EventArgs e)
        {
            strWhere = " 1=1  ";//AND CONVERT(datetime, Update_Time)>'2021-01-01'
            if (!string.IsNullOrEmpty(txt_RU.Text.Trim()))
            {
                this.strWhere += "   AND PutList_NO like '%" + txt_RU.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txt_point.Text.Trim()))
            {
                this.strWhere += "   AND Posiont1 like '%" + txt_point.Text.Trim() + "%'";
            }
            if (checkBox1.Checked)
            {
                if (!string.IsNullOrEmpty(txt_machine.Text.Trim()))
                {
                    this.strWhere += "   AND   Replace(Product_Model,' ','')  LIKE '%" + txt_machine.Text.Trim() + "%'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(txt_machine.Text.Trim()))
                {
                    this.strWhere += "   AND   Replace(Product_Model,' ','')  = '" + txt_machine.Text.Trim() + "'";
                }
            }

            if (!string.IsNullOrEmpty(txt_Seri.Text.Trim()))
            {
                this.strWhere += "   AND Serial_Number like '%" + txt_Seri.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(comboBox2.Text))
            {
                this.strWhere += "  AND  BK03='" + comboBox2.Text.Trim() + "' ";
            }

            this.strWhere += " AND  PickList_NO IS  NULL AND BK03!='1'  AND BK11 IS NULL AND BK06<>0 ";
            this.pageControl2.Bind();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataTable dt;
            string SelectSQL = "SELECT [PutList_NO] AS '入库单号',[Posiont1] AS '库位',[Product_Model] AS '机型',[Serial_Number] AS '序列号',[BK06] AS '库存数量' ,[BK03] AS '分类' ,[Update_Time] AS '操作时间'  FROM [T_Stock]  ";
            if (strWhere != "") SelectSQL += "WHERE  " + strWhere + "    ";
            dt = DbHelperSQL.ExecuteDataTable(SelectSQL);
            ExPut.GetNopiExcel(dt);
        }

        private void btn_PROAV_Click(object sender, EventArgs e)
        {
            Frm_PROAV frm = new Frm_PROAV();
            frm.ShowDialog();
        }

        private int _NID = 0;
        private string _Type = "";

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            _NID = 0;
            if (dataGridView2.Rows.Count > 0)
            {
                if (e.RowIndex > -1)
                {
                    if (!SystemVariable.VaildationPermission("编辑")) return;
                    _NID = Convert.ToInt32(dataGridView2.CurrentRow.Cells["NID2"].Value.ToString());
                    _Type = dataGridView2.CurrentRow.Cells["BK03"].Value.ToString();
                    if (_NID > 0)
                    {
                        if (_Type == "OPTION" || _Type == "Option")
                        {
                            List_StockUpdateOptionFrm frm = new List_StockUpdateOptionFrm(_NID.ToString());
                            frm.ShowDialog();
                            pageControl2.Bind();
                            _NID = 0;
                        }
                    }
                }
            }
        }
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
    }
}
